<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/11/18
 * Time: 下午9:55
 */

class ModGame extends Model {

    private $table = 'tblGame';

    // 表名
    public function tableName(){
        return $this->table;
    }

    public static function model($className=__CLASS__){
        return parent::model($className);
    }

    public function getDbConnection(){
        return Yii::app()->dbAuth;
    }

    public function select($params = array()){

        $criteria = new CDbCriteria();
        if(!empty($params['search'])){
            $criteria->addSearchCondition('GameName', $params['search']);
        }
        $criteria->order = ' GameSrc ASC ';

        $tmp = $this->findAll($criteria);

        $result = array();
        if(!empty($tmp)){
            foreach($tmp as $v){
                $result[] = array(
                    'GameID'     => $v->GameID,
                    'GameIcon'   => $v->GameIcon,
                    'GameSrc'    => $v->GameSrc,
                    'GameName'   => $v->GameName,
                    'Remark'     => $v->Remark
                );
            }
        }

        return $result;
    }

    public function getByUserRole($userId){
        $sql = 'SELECT GameID,GameSrc,GameIcon,GameName,Remark FROM tblGame WHERE GameID IN (
                  SELECT DISTINCT GameID FROM tblUserRoleMap WHERE UserID=:UserID
                )';

        $result = $this->getDbConnection()->getAll($sql, array(':UserID' => $userId));

        return $result;
    }

    public function add($game){
        $this->insert();
    }
} 
